<?php 
////////////////////////////////////////////////////////////////////////
/////// START 
////////////////////////////////////////////////////////////////////////
foreach ($argv as $argument) {
	if ($argument=='--help') exit("php file.php --config=[config file] --debug=[debug]  --list=[url list]\n");
    $argParts = split('=', $argument);
    switch ($argParts[0]) {
        case "--config";
			$config = $argParts[1];
        break;
        case "--debug";
			$level = $argParts[1];
        break;
        case "--domain";
			$domain = $argParts[1];
        break;
    }
}

if ($domain == null) $domain="domain.txt";
//if ($level == null) $level=0;

$sites=file($domain);
// format is url;[port;port;port]
// example 
// http://www.ferzip.com/test.php;21;25;5269
// check the url http://www.ferzip.com/test.php; ftp server; mail server and jabber
if ($config == null){
// config mail
$Name = "SITE CHECK"; //senders name
$email = "check@server.com"; //senders e-mail adress
$recipient = "tech@server.com"; //recipient
$subject = "ALERT check ".date('Y-m-d H:i:s'); //subject

}
/*
 * $debug=1 //if exist don't send email only show 
 */
 


////////////////////////////////////////////////////////////////////////
// MAIN PROGRAMM
////////////////////////////////////////////////////////////////////////

foreach ($sites as $site) {
	$check=explode(";",$site);
	$domain=$check[0];
	if (strpos($domain,"#")===false){
		
		$httpcode=web($domain);
		if($httpcode>=200 && $httpcode<300) 		
				   $msg1.= " Website OK\n";
			 else{
				$msg= "\n\n Start check ".getHost($domain) ."\n =========================== \n".date('Y-m-d H:i:s')."\n";
				$msg.= " Website DOWN  : ".$httpcode."\n";
			}
		
		for($i=1;$i<sizeof($check);$i++) 
				$msg.=service(getHost($domain),$check[$i]); 
				
		if($debug)
			echo $msg;
		else
		    $mail.=$msg;
	}
}
	if(!$debug)
	  if($msg)
		mail($recipient, $subject, $mail); //mail command :)
  
  
  
////////////////////////////////////////////////////////////////////////
//////////////  FUNCTION 
////////////////////////////////////////////////////////////////////////
function web($url){
	$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0; web-check )";$ch=curl_init();
	curl_setopt ($ch, CURLOPT_URL,$url );
	curl_setopt($ch, CURLOPT_USERAGENT, $agent);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch,CURLOPT_VERBOSE,false);
	curl_setopt($ch, CURLOPT_TIMEOUT, 5);
	$page=curl_exec($ch);
	//echo curl_error($ch);
	$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);
	return $httpcode;
	
}
function service($url,$port){
     $fp = @fsockopen($addr, $port, $errno, $errstr, 2);
     $msg = $port ." is closed and cannot be used at this time\n";
      if ($fp) {        
        $msg = $port ." is open and ready to be used\n";
      }
      @fclose($fp);
	return $msg; 
}

function getHost($Address) {
   $parseUrl = parse_url(trim($Address));
   return trim($parseUrl[host] ? $parseUrl[host] : array_shift(explode('/', $parseUrl[path], 2)));
}
      
?>




